package 力扣_算法题综合.高频面试算法;

import java.util.ArrayDeque;

public class 约瑟夫环 {
    public static void main(String[] args) {
        findTheWinner(8, 3);
    }

    /**
     * 队列默认尾插头删
     */
    public static int findTheWinner(int n, int k) {
        ArrayDeque<Integer> queue = new ArrayDeque<>();
        for (int i = 1; i <= n; i++) {
            queue.offer(i);
        }
        while (queue.size() > 1) {
            for (int i = 1; i < k; i++) {
                queue.offerLast(queue.pollFirst());
            }
            queue.poll();
        }
        return queue.peek();
    }
}
